import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';

import set from '@dfeidao/atom-web/local/set';
import render from '@dfeidao/atom-web/render/render';
// import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
// import Tree from '@dfeidao/fd-w000001';
// import a005 from './a005';
import tree from '@dfeidao/fd-w000001';
import P06 from './P06';

// import P04 from './P04';
export default async function a010(fd: IFeidaoAiBrowserComponent, args?: { data: { data: { formid: string, client_type: string, create_time: number, group_number: string; formname: string, package_no: string, package_name: string, package_path: string, productid: string, group_name: string, no: string } } }) {
	// 折叠树绑定事件
	const tenantid = get(fd, 'tenantid') as string;
	const productid1 = get(fd, 'productid') as string;

	if (!tenantid) {
		error('请先选择租户！');
		return;
	} else {
		const zds = fd.data.node.querySelector('[zds]') as tree;

		// debugger;
		const a = get(fd, 'origin_obj') as Map<string, { left: {}, right: Array<{}> }>;
		// console.log(a, 'dd');
		const checked = zds.get_checked() as Array<{
			group_name: string, group_number: string, no: string, formid: string
		}>;
		// console.log(checked, 'checked');
		const data = args.data.data;
		// let flag = 'true';
		// console.log(data, 'data');
		const group_number = data.group_number;
		const select1 = fd.data.node.querySelector<HTMLSelectElement>('[select]');
		const index = select1.options.selectedIndex;
		const client_type1 = select1.options[index].value;

		const res1 = await nodejs<{ dataArray: Array<{ formid: string }> }>('tenant-function/zj-000001/s006', { filter: { productid: productid1, client_type: client_type1, group_number } });
		// debugger;
		// console.log(res1, 'res1');

		// console.log(all_data, 'all_data');
		if (!data.no) {
			const quanxuanarr = new Array();
			checked.forEach((v: { group_name: string, group_number: string, no: string }) => {
				if (v.group_name === data.group_name && v.no) {
					quanxuanarr.push(v);
				}
			});
			let d_data = new Array();
			quanxuanarr.forEach((o) => {
				d_data = checked.filter((nm) => {
					return nm.formid === o.formid;
				});
			});
			// console.log(d_data);
			if (d_data.length !== 0) {
				// console.log('bbb');

				quanxuanarr.forEach((i) => {

					if (i.group_name === "未分组") {
						i.group_name = '';
						i.group_number = '';
					}
					if (Array.from(a.keys()).indexOf(i.formid) === -1) {
						a.set(i.formid, { left: { client_type: i.client_type, create_time: i.create_time, formid: i.formid, formname: i.formname, group_number: i.group_number, group_name: i.group_name, package_no: i.package_no, package_name: i.package_name, package_path: i.package_path, productid: tenantid }, right: [] });
					} else {
						const te = a.get(i.formid);
						te.left = { client_type: i.client_type, create_time: i.create_time, formid: i.formid, formname: i.formname, group_number: i.group_number, group_name: i.group_name, package_no: i.package_no, package_name: i.package_name, package_path: i.package_path, productid: tenantid };
					}
				});
			} else {
				// console.log(quanxuanarr, 'quanxuanarr');
				// // data.no
				res1.dataArray.forEach((j) => {

					a.delete(j.formid);

				});
				render(fd, '', P06, "P06", "inner");

			}

			// console.log(a);
		} else {
			const c_data = checked.filter((nm) => {
				return nm.formid === data.formid;
			});
			if (c_data.length !== 0) {
				// console.log(args, '333');

				const res = await nodejs<{ dataArray: Array<{ ode: string, formid: string }> }>('tenant-function/zj-000001/s005', { filter: { productid: productid1, formid: data.formid } });
				render(fd, res.dataArray, P06, "P06", "inner");
				// const res2 = await nodejs<{ dataArray: Array<{ code: string, formid: string }> }>('tenant-function/zj-000001/s005', { filter: { productid: tenantid, formid: data.formid } });
				res.dataArray.forEach((i) => {
					if (Array.from(a.keys()).indexOf(i.formid) === -1) {
						a.set(i.formid, {
							left: {},
							right: [i]
						});
					} else {
						const te = a.get(i.formid);
						te.right.push(i);
					}
				});
				if (Array.from(a.keys()).indexOf(data.formid) === -1) {
					a.set(data.formid, { left: { client_type: data.client_type, create_time: data.create_time, formid: data.formid, formname: data.formname, group_number: data.group_number, group_name: data.group_name, package_no: data.package_no, package_name: data.package_name, package_path: data.package_path, productid: tenantid }, right: [] });
				} else {
					const te = a.get(data.formid);
					te.left = { client_type: data.client_type, create_time: data.create_time, formid: data.formid, formname: data.formname, group_number: data.group_number, group_name: data.group_name, package_no: data.package_no, package_name: data.package_name, package_path: data.package_path, productid: tenantid };
				}

			} else {
				render(fd, '', P06, "P06", "inner");
				a.delete(data.formid);


			}
		}
		// console.log(a);

		set(fd, 'origin_obj', a);


	}



}
